<template>
  <div class="w-full h-full bgMeau">
    <Nav class="fixed top-0 w-full z-10" />
    <div class="mt-10 bodyPanel">
      <van-empty v-if="panelData.length == 0" :description="translateTitle('Base.40xPage.此用户无权限')" image-size="10rem" />
      <MenuList v-else :panel-data="panelData" @clickitem="clickItem" />
    </div>
  </div>
</template>

<script>
  import { defineComponent, reactive, toRefs, computed } from 'vue'
  import isEmpty from 'lodash/isEmpty'
  import { useRouter } from 'vue-router'
  import { useStore } from 'vuex'
  import Nav from '../components/nav'
  import MenuList from '../components/MenuList2'
  import { Empty } from 'vant'
  import { damsPost } from '../api/api.js'
  import { translateTitle } from '@/utils/i18n'

  export default defineComponent({
    name: 'MenuPage',
    components: { MenuList, Nav, [Empty.name]: Empty },
    setup() {
      let Panel = reactive({})
      const store = useStore()
      // 进入页面时清除之前打开页面的记录 防止多页面同时存在影响当前页面的判断（会加载多余的页面json）
      store.dispatch('tabs/delAllVisitedRoutes')
      store.dispatch('preview/initPages')
      // 自定义导航栏admin
      store.dispatch('pdaNav/setnavData', {
        title: translateTitle('Base.login.功能面板'),
        outLogin: false,
        notMenu: false,
      })
      const token = store.getters['user/token']
      if (isEmpty(token)) {
        store.dispatch('user/logout')
      }
      // 事件
      const router = useRouter()
      function clickItem(item) {
        console.log('item.path', item.path)
        router.push(`${item.path}`)
      }

      return {
        ...toRefs(Panel),
        clickItem,
        // init,
      }
    },
    data() {
      return {
        panelData: [],
      }
    },
    created() {
      if (this.$route.query.webTk) {
        // web端跳转过来的
        this.webGetTk()
      } else {
        this.init()
      }
    },
    methods: {
      translateTitle,
      //初始化
      init() {
        // const routes = computed(() => this.$store.getters['routes/routes'])

        // let _routers = routes.value.filter((v) => {
        //   return v.meta && !v.meta.hidden && v.name !== 'Dashboard' && v.children
        // })
        const _routers = computed(() => this.$store.getters['user/menu'])
        _routers.value.forEach((e) => {
          if (e.children && e.children.length > 0) {
            e.children.forEach((ee) => {
              ee.path = `${e.path}/${ee.path}`
            })
          }
        })
        console.log('routes', _routers)
        this.panelData = _routers
        // 重置所有界面数据WWWWW
        // const storePages = usePagesStore()
        // storePages.resetPages()
        //暂时没发现啥作用 先隐藏 bobby
      },
      //web端跳转过来获取新的token的接口
      async webGetTk() {
        let method = 'sysUserServiceImpl.loginForPda'
        const res = await damsPost({ LOGIN_ENTRANCE: 'PDA' }, method)
        if (isEmpty(res)) {
          this.$router.push('/')
        } else {
          console.log('web端跳转过来获取新的token的接口', res)
          this.$store.dispatch('user/setToken', res.data.token)
          await this.$store.dispatch('user/getUserInfo', {
            appCode: 'WMS',
            ORG_ID: this.$store.getters['user/orgCode'],
            WAREHOUSE_ID: this.$store.getters['user/wareHoseId'],
            demo: '123123',
            token_reset: 'Y',
          })

          await this.$store.dispatch('routes/setRoutes', 'none')

          setTimeout(() => {
            this.init()
          }, 1)
        }
      },
    },
  })
</script>

<style lang="scss">
  .bgMeau {
    background: #efeded !important;
  }
  .bodyPanel {
    margin-top: 3rem !important;
    border: 1px solid #fff !important;
    background: #fff !important;
    // height: 100vh !important;
    // overflow-y: auto !important;
    // padding-bottom: 4rem !important;
    // &::-webkit-scrollbar {
    //   display: none;
    // }
  }
  //面板颜色
  .van-nav-bar__content {
    color: #fff !important;
  }

  .PanelFuc {
    height: 100vh !important;
    overflow-y: auto !important;
    padding-bottom: 4rem !important;
    &::-webkit-scrollbar {
      display: none;
    }
  }
</style>
